<template>
  <div class="login-container">
       <vue-particles
        color="#fff"
        :particleOpacity="0.7"
        :particlesNumber="60"
        shapeType="circle"
        :particleSize="4"
        linesColor="#fff"
        :linesWidth="1"
        :lineLinked="true"
        :lineOpacity="0.4"
        :linesDistance="150"
        :moveSpeed="2"
        :hoverEffect="true"
        hoverMode="grab"
        :clickEffect="true"
        clickMode="push"
        class="lizi"
      >
      </vue-particles>
    <div class="login-weaper animated bounceInDown" style="z-index:100">
      <div class="login-left">
        <div class="login-time">
          {{time}}
        </div>
        <img class="img"
             :src="logo"
             alt="">
        <p class="title">{{productName}}</p>
      </div>
      <div class="login-border">
        <div class="login-main">
          <h4 class="login-title">
           用户登录
          </h4>
            <el-form ref="inputForm" :model="inputForm" :rules="rules" label-width="80px" @keyup.enter.native="login()" @submit.native.prevent>
              <el-form-item label="账号" prop="username">
                <el-input type="text" placeholder="请输入账号" v-model="inputForm.username"/>
              </el-form-item>
              <el-form-item label="密码" prop="password">
                <el-input type="password" placeholder="请输入密码" v-model="inputForm.password"/>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" :loading="loading" class="login-submit" @click="login">登录</el-button>
              </el-form-item>
            </el-form>
        </div>
      </div>
    </div>

    <el-dialog
      title="请选择项目"
      :fullscreen = "true"
      :visible.sync="dialogVisible"
      width="70%">
      <div class="uni-flex">
        <el-card class="uni-project-card" v-for="project in projectList" :key="project.projectId">
          <div slot="header" class="clearfix">
            <span>{{project.name}}</span>
            <el-button style="float: right; padding: 3px 0" type="text" @click="gotoProject(project)">进入</el-button>
          </div>
          <div class="uni-cell">
            项目编号：{{project.projectId}}
          </div>
          <div class="uni-cell">
            项目类型：{{project.type}}
          </div>
          <div class="uni-cell">
            创建时间：{{project.createDate}}
          </div>
          <div class="uni-cell">
            项目地址：{{project.addr}}
          </div>
        </el-card>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    data () {
      return {
        time: '',
        logo: '',
        productName: '',
        loading: false,
        dialogVisible: false,
        projectList: [],
        inputForm: {
          username: '',
          password: '',
          uuid: '',
          captcha: ''
        },
        rules: {
          username: [
            {required: true, message: '帐号不能为空', trigger: 'blur'}
          ],
          password: [
            {required: true, message: '密码不能为空', trigger: 'blur'}
          ],
          captcha: [
            {required: true, message: '验证码不能为空', trigger: 'blur'}
          ]
        },
        captchaPath: ''
      }
    },
    created () {
      this.getTime()
      setInterval(() => {
        this.getTime()
      }, 1000)
    },
    mounted () {
      this.tokenLogin()
      this.$http.get('/sys/sysConfig/queryById').then(({data}) => {
        this.logo = data.config.logo
        this.productName = data.config.productName
				document.title = data.config.productName
        this.$store.commit('config/updateConfig', data.config);
      })
    },
    methods: {
      // 提交表单
      login () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: '/sys/login',
              method: 'post',
              useAes: true,
              data: {
                'userName': this.inputForm.username,
                'password': this.inputForm.password
              }
            }).then(({data}) => {
              if (data && data.success) {
                this.$cookie.set('token', data.token)
                this.$cookie.set('refreshToken', data.refreshToken)
                this.gotoNext(data.projectList || [])
                this.loading = false
              } else {
                this.loading = false
              }
            })
          }
        })
      },
      tokenLogin () {
        if(!this.$route.query.userName || this.$route.query.userName === '') {
          return
        }
        if(!this.$route.query.timeStamp || this.$route.query.timeStamp === '') {
          return
        }
        if(!this.$route.query.token || this.$route.query.token === '') {
          return
        }
        this.loading = true
        this.$http({
          url: '/sys/tokenLogin',
          method: 'post',
          data: {
            'userName': this.$route.query.userName,
            'timeStamp': this.$route.query.timeStamp,
            'token': this.$route.query.token
          }
        }).then(({data}) => {
          if (data && data.success) {
            this.$cookie.set('token', data.token)
            this.$cookie.set('refreshToken', data.refreshToken)
            this.gotoNext(data.projectList || []);
            this.loading = false
          } else {
            this.loading = false
          }
        })
      },
      gotoNext(projectList) {
        if(projectList.length == 0) {
          this.$message.warning("该用户账号下无项目");
        } else if(projectList.length == 1) {
          this.gotoProject(projectList[0]);
        } else {
          this.dialogVisible = true;
          this.projectList = projectList;
        }
      },
      gotoProject(project) {
        this.dialogVisible = false;
        this.$store.commit('config/updateProjectId', project.projectId);
        this.$store.commit('config/updateProjectName', project.name);
        this.$router.push({name: 'home'})
      },
      getTime () {
        this.time = this.moment(new Date()).format('YYYY-MM-DD HH:mm:ss')
      },
      // 获取验证码
      getCaptcha () {
        this.captchaPath = this.$http.BASE_URL + `/captcha.jpg`
      }
    }
  }
</script>

<style lang="scss">
  @import '~@/assets/scss/login.scss';
  .uni-project-card {
    width: 360px;
    margin: 20px;
    .uni-cell {
      line-height: 30px;
    }
  }
  .login-container {
    // .el-dialog {
    //   background: url("/static/img/login.png");
    //   background-color: #0e6cff;
    // }
    .el-dialog__body {
      height: 100%;
      position: relative;
      padding: 0;
    }
  }
</style>
